namespace PaulRz.Util.Patterns
{
    /// <summary>
    /// Worker pipeline element with possibility to generate several messages
    /// </summary>
    /// <typeparam name="TInput"></typeparam>
    /// <typeparam name="TOutput"></typeparam>
    public abstract class MultiWorkerPipelineElement<TInput, TOutput> : PipelineElement<TInput, TOutput>
        where TInput : class
        where TOutput : class 
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="name"></param>
        /// <param name="logger"></param>
        /// <param name="joinTime"></param>
        protected MultiWorkerPipelineElement(string name, ILogInterface logger, int joinTime) : base(name, logger, joinTime)
        {
        }

        /// <summary>
        /// The main processing method
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected abstract TOutput[] ProcessMessage(TInput message);
        
        /// <summary>
        /// Processing implementation
        /// </summary>
        protected override void ProcessCycle()
        {
            TInput message = Input.Dequeue();
            TOutput[] processedMessage = ProcessMessage(message);
            if (processedMessage!=null)
                foreach (TOutput output in processedMessage)
                {
                    Output.Enqueue(output);
                }
        }
    }
}
